<?php
class Auth extends MY_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->lang->load('form_validation', 'french');
	}
    function login()
	{
		
		if($this->member){
			redirect('account');
		}else{
		$data = array();
		//if($this->member) redirect('home');
		if($_POST)
		{
			// The user has submitted the login form
			
			$member = member::validate_login($_POST['email'],$_POST['password']);
			
			if($member){
				//echo $this->uri->segment(2);
				if($this->uri->segment(2)==NULL){
					redirect('menu');
				}else{
					redirect($this->uri->segment(2));
				}
		
			}else{
				
		   		 $data['message']= "La combinaison ( utilisateur / mot de passe ) que vous avez introduit n'est pas valable.";
			}
		}
		//if(isset($this->member)) $data['member'] = $this->member;
		$data['title'] = $this->title('Connexion');
		$data['menu'] = cat::find('all',array('conditions'=>array('parent_id'=>NULL)));
		$data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
		$data['files'] = get_dir_file_info('lunch_pdf', $top_level_only = TRUE);
		$this->load->view('header',$data);
	   $this->load->view('auth/login',$data);
	   $this->load->view('footer',$data);
		}
	}
	
	function logout()
	{
	  member::logout();
	  
	
	}
	
	function inscription()
	{
		if($this->member){
			redirect('account');
		}else{
		 $data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
		 $data['files'] = get_dir_file_info('lunch_pdf', $top_level_only = TRUE);
		 $data['menu'] = cat::find('all',array('conditions'=>array('parent_id'=>NULL)));
		 if($_POST)
		 {
		 	$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[members.email]');
		 	$this->form_validation->set_rules('firstname', 'Prénom', 'required');
		 	$this->form_validation->set_rules('lastname', 'Nom', 'required');
		 	$this->form_validation->set_rules('tel', 'Tél.', 'required');
		 	$this->form_validation->set_rules('password', 'Mot de passe', 'required|matches[passconf]');
		 	$this->form_validation->set_rules('passconf', 'Confirmation de mot de passe', 'required');
		 	if ($this->form_validation->run() == FALSE)
		 	{
		 		$data['msg_error'] = $this->form_validation->set_message('rule', 'Error Message');
		 		$data['title'] =$this->title('Inscription');
		 		$this->load->view('header',$data);
		 		$this->load->view('auth/inscription',$data);
		 		$this->load->view('footer',$data);
		 	}
		 	else
		 	{
		 		$new_member = member::registration($_POST['email'],$_POST['password'],$_POST['firstname'],$_POST['lastname'],$_POST['tel'],$_POST['entreprise']);
		 		if($new_member == true){
		 			redirect('menu');
		 				
					}
		 	}
		 }else{
	  		
		 	$data['title'] =$this->title('Inscription');
		 	$this->load->view('header',$data);
		 	$this->load->view('auth/inscription',$data);
		 	$this->load->view('footer',$data);
		 }
		}
	}
 	public function email_check($str)
			{	
				
				$email = member::find_by_email($str);
				if($email){
					return TRUE;
				}else{
					
   					return FALSE;
					
				}
			}
			
			
			function forgot_password()
			{	if($this->member){
				redirect('account');
			}else{
				$data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
				$data['files'] = get_dir_file_info('lunch_pdf', $top_level_only = TRUE);
				$data['menu'] = cat::find('all',array('conditions'=>array('parent_id'=>NULL)));
				$this->lang->load('form_validation', 'french');
				if($_POST)
				{
					$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check');
						
						
					if ($this->form_validation->run() == FALSE)
					{
							
						$data['title'] = 'sushibox | Mot de Passe';
						$this->load->view('header',$data);
						$this->load->view('auth/forgot_password',$data);
						$this->load->view('footer',$data);
					}
					else
					{

						$data['m'] = member::find_by_email($_POST['email']);
						$config['protocol'] = 'mail';
						$config['wordwrap'] = FALSE;
						$config['mailtype'] = 'html';
						$config['charset'] = 'utf-8';
						$config['crlf'] = "\r\n";
						$config['newline'] = "\r\n";
						$this->email->initialize($config);
						$this->email->from('no-reply@neufneufdragon.be', 'www.neufneufdragon.be');
						$this->email->to($_POST['email']);
						$this->email->subject("Nouveau Mot de Passe");
						$email = $this->load->view('emails/newpassword',$data,TRUE);
						$this->email->message($email);
						if($this->email->send())
						{
							$data['message'] = "Nouveau mot de passe est envoyé.";
							$data['title'] = $this->title('Mot de Passe');
							$data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
							$this->load->view('header',$data);
							$this->load->view('auth/forgot_password',$data);
							$this->load->view('footer',$data);
						}
					}
						
						
						
				}else{
					$data['title'] = $this->title('Mot de Passe');
					$data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
					$this->load->view('header',$data);
					$this->load->view('auth/forgot_password',$data);
					$this->load->view('footer',$data);

				}

			}

			}
	
	public function set_newpwd()
	{
		$data['menu'] = cat::find('all',array('conditions'=>array('parent_id'=>NULL)));
	    $data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
	    $data['files'] = get_dir_file_info('lunch_pdf', $top_level_only = TRUE);
		if($_GET)
		{			
			$check_member = member::find_by_key_set($_GET['key']);
			if($check_member){
			
					 $this->form_validation->set_rules('password', 'Mot de passe', 'required|matches[passconf]');
					 $this->form_validation->set_rules('passconf', 'Confirmation de mot de passe', 'required');
					 if ($this->form_validation->run() == FALSE)
 					 {
   					 
	   				 $data['title'] = $this->title('Mot de Passe');
	   				 $this->load->view('header',$data);
	                 $this->load->view('auth/set_newpwd',$data);
	                 $this->load->view('footer',$data);
  			         }else{
  			         
  			         $new_pwd = member::set_newpwd($check_member->id,$_POST['password'],$check_member->email);
  			         if($new_pwd==true){
					 	redirect('login');
  			         }
  			         }
			
			}else{
  			
  					 $data['title'] = $this->title('Mot de Passe');
	   				 $this->load->view('header',$data);
	                 $this->load->view('auth/set_newpwd_error',$data);
	                 $this->load->view('footer',$data);
			}
  			
		}else{
			
			         $data['title'] = $this->title('Mot de Passe');
	   				 $this->load->view('header',$data);
	                 $this->load->view('auth/set_newpwd_error',$data);
	                 $this->load->view('footer',$data);
			
		}
		
		
	}
	
	public function check_login()
	{
		if(!$this->member){
			echo json_encode(false) ;
		}else{
			echo json_encode(true);
		}
	}

}